//
//  RBFactoryMethod.m
//  FMLApp
//
//  Created by Riber on 2017/4/1.
//  Copyright © 2017年 Riber. All rights reserved.
//

#import "RBFactoryMethod.h"

@implementation RBFactoryMethod

+ (UILabel *)initWithLabelFrame:(CGRect)frame andTextAlignment:(NSTextAlignment)textAlignment andTextColor:(UIColor *)color fontOfSystemSize:(CGFloat)fontSize isBold:(BOOL)isBold
{
    UILabel *factoryLabel = [[UILabel alloc] initWithFrame:frame];
    factoryLabel.textColor = color;
    if (isBold) {
        factoryLabel.font = [UIFont boldSystemFontOfSize:fontSize];
    }
    else
    {
        factoryLabel.font = [UIFont systemFontOfSize:fontSize];
    }
    factoryLabel.textAlignment = textAlignment;
    
    return factoryLabel;
}

+ (UILabel *)initWithMasonryLabelTextAlignment:(NSTextAlignment)textAlignment andTextColor:(UIColor *)color fontOfSystemSize:(CGFloat)fontSize isBold:(BOOL)isBold
{
    UILabel *factoryLabel = [[UILabel alloc] init];
    factoryLabel.textColor = color;
    if (isBold) {
        factoryLabel.font = [UIFont boldSystemFontOfSize:fontSize];
    }
    else
    {
        factoryLabel.font = [UIFont systemFontOfSize:fontSize];
    }
    factoryLabel.textAlignment = textAlignment;
    
    return factoryLabel;
}

+ (UIImageView *)initWithNormalImageViewFrame:(CGRect)frame andImageName:(NSString *)name
{
    UIImageView *factoryImageView = [[UIImageView alloc] initWithFrame:frame];
    
    if (name.length != 0) {
        factoryImageView.image = [[UIImage imageNamed:name] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    return factoryImageView;
}

+ (UIImageView *)initWithNormalMasonryImageViewWithImageName:(NSString *)imageName
{
    UIImageView *factoryImageView = [[UIImageView alloc] init];
    
    if (imageName.length != 0) {
        factoryImageView.image = [UIImage imageNamed:imageName];
    }
    
    return factoryImageView;
}

+ (void)initWithGuideImageViewTarget:(id)target Name:(NSString *)imageName buttonFrame:(CGRect)frame
{
    UIImageView *factoryImageView = [[UIImageView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    factoryImageView.userInteractionEnabled = YES;
    factoryImageView.image = [UIImage imageNamed:imageName];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    //    button.backgroundColor = [UIColor redColor];
    [button addTarget:target action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [factoryImageView addSubview:button];
    
    //    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:@selector(tapImageView:)];
    //    [factoryImageView addGestureRecognizer:tap];
    
    [[UIApplication sharedApplication].keyWindow addSubview:factoryImageView];
}

+ (UIView *)initWithLineViewFrame:(CGRect)frame
{
    UIView *lineView = [[UIView alloc] initWithFrame:frame];
    lineView.backgroundColor = [UIColor colorWithRed:0.922 green:0.922 blue:0.922 alpha:1.00];
    
    return lineView;
}

+ (UIButton *)initWithNormalButtonFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)color fontOfSystemSize:(CGFloat)fontSize
{
    UIButton *factoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    factoryButton.frame = frame;
    [factoryButton setTitle:title forState:UIControlStateNormal];
    [factoryButton setTitleColor:color forState:UIControlStateNormal];
    factoryButton.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    
    return factoryButton;
}

+ (UIButton *)initWithNormalImageButtonFrame:(CGRect)frame normalImageName:(NSString *)normalImageName selectImageName:(NSString *)selectImageName {
    UIButton *factoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    factoryButton.frame = frame;
    if (!([normalImageName isEqualToString:@""] || normalImageName == nil)) {
        [factoryButton setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
    }
    if (!([selectImageName isEqualToString:@""] || selectImageName == nil)) {
        [factoryButton setImage:[UIImage imageNamed:selectImageName] forState:UIControlStateSelected];
    }

    return factoryButton;
}


+ (UITextField *)initWithTextFieldFrame:(CGRect)frame textAlignment:(NSTextAlignment)textAlignment textColor:(UIColor *)color placeHoder:(NSString *)placeHoder fontOfSystemSize:(CGFloat)fontSize
{
    UITextField *factoryTF = [[UITextField alloc] initWithFrame:frame];
    factoryTF.placeholder = placeHoder;
    factoryTF.textColor = color;
    factoryTF.textAlignment = textAlignment;
    factoryTF.font = [UIFont systemFontOfSize:fontSize];
    //[factoryTF setValue:color forKeyPath:@"_placeholderLabel.textColor"];
    [factoryTF setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:placeHoder attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}]];
//    [factoryTF setValue:[UIFont systemFontOfSize:fontSize] forKeyPath:@"_placeholderLabel.font"];
    
    return factoryTF;
}

+ (UITextField *)initWithMasonryTextFieldTextAlignment:(NSTextAlignment)textAlignment textColor:(UIColor *)color placeHoder:(NSString *)placeHoder fontOfSystemSize:(CGFloat)fontSize {
    UITextField *factoryTF = [[UITextField alloc] init];
    factoryTF.placeholder = placeHoder;
    factoryTF.textColor = color;
    factoryTF.textAlignment = textAlignment;
    factoryTF.font = [UIFont systemFontOfSize:fontSize];
    //[factoryTF setValue:color forKeyPath:@"_placeholderLabel.textColor"];
    [factoryTF setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:placeHoder attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}]];
    //    [factoryTF setValue:[UIFont systemFontOfSize:fontSize] forKeyPath:@"_placeholderLabel.font"];
    
    return factoryTF;
}


+ (NSMutableAttributedString *)addAttributed:(NSString *)oldString attributes:(NSDictionary *)attributesDic range:(NSRange)range
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:oldString];
//    [attributedString addAttributes:attributesDic range:range];
    [attributedString setAttributes:attributesDic range:range];
    
    return attributedString;
}

+ (void)alertViewOrControllerShow:(NSString *)alertString viewController:(UIViewController *)viewController
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:alertString delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
    }
    else
    {
        UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:@"" message:alertString preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertViewController addAction:action];
        [viewController presentViewController:alertViewController animated:YES completion:nil];
    }
}

+ (void)alertWithTitle:(NSString *)title message:(NSString *)message forViewController:(UIViewController *)viewController okHandler:(void (^ __nullable)(UIAlertAction *action))okHandler cancelHandler:(void (^ __nullable)(UIAlertAction *action))cancelHandler {
    UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:cancelHandler];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:okHandler];
    [cancel setValue:[UIColor redColor] forKey:@"titleTextColor"];
    
    [alertViewController addAction:cancel];
    [alertViewController addAction:action];
    
    
    // 设置样式
    NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
    [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, title.length)];
    [alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, title.length)];
    [alertViewController setValue:alertControllerStr forKey:@"attributedTitle"];
    
    [viewController presentViewController:alertViewController animated:YES completion:nil];
}

- (void)tapImageView:(UITapGestureRecognizer *)tap {
    
}

- (void)buttonClick:(UIButton *)button {
    
}

+ (NSString *)getAppInfoWithAppNameOrVersionOrBuild:(NSString *)type
{
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app名称
    NSString *appName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    // app版本
    NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    // app build版本
    NSString *appBuild = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    if ([type isEqualToString:@"name"]) {
        return appName;
    }
    else if ([type isEqualToString:@"version"])
    {
        return appVersion;
    }
    else if ([type isEqualToString:@"build"]) {
        return appBuild;
    }
    else
    {
        return @"参数错误";
    }
}

+ (void)callPhone:(NSString *)phone showView:(nonnull UIView *)showView{
    NSString *urlString = [NSString stringWithFormat:@"tel:%@", phone];
    UIWebView *callWebView = [[UIWebView alloc] init];
    [callWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    [showView addSubview:callWebView];
}

@end
